package cn.schoolwow.flow.monitor.config;

import cn.schoolwow.ams.domain.block.router.RouterBlockBuilder;
import cn.schoolwow.ams.domain.option.QuickAMSListener;
import cn.schoolwow.flow.monitor.domain.FlowMonitorOption;
import cn.schoolwow.flow.monitor.module.business.configParameter.aware.ConfigParameterAware;
import cn.schoolwow.flow.monitor.module.business.flowLog.aware.ExceptionFlowLogAware;
import cn.schoolwow.flow.monitor.module.business.flowLog.aware.FlowLogAware;
import cn.schoolwow.flow.monitor.module.business.flowRecord.aware.FlowRecordAware;
import cn.schoolwow.flow.monitor.module.business.scheduleJob.definition.aware.ScheduleJobAware;
import cn.schoolwow.flow.monitor.module.business.scheduleJob.definition.aware.ScheduleJobLogAware;
import cn.schoolwow.flow.monitor.module.business.testcase.aware.TestCaseAware;
import cn.schoolwow.flow.monitor.module.business.testcase.aware.TestCaseLogAware;
import cn.schoolwow.flow.monitor.module.common.domain.FlowMonitorConstant;
import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.util.domain.check.instance.CheckInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@ComponentScan(basePackages = "cn.schoolwow.flow.monitor")
@Configuration
public class FlowMonitorConfig {
    @Bean
    @Autowired
    public QuickAMSListener flowMonitorQuickAMSListener(ApplicationContext applicationContext){
        QuickAMSListener quickAMSListener = new QuickAMSListener() {
            @Override
            public void addRouterBlockArray(RouterBlockBuilder routerBlockBuilder) {
                routerBlockBuilder.children("/log", "流程日志")
                        .block(applicationContext.getBean(FlowLogAware.class))
                        .block(applicationContext.getBean(ExceptionFlowLogAware.class));
                routerBlockBuilder.children("/config", "项目配置")
                        .block(applicationContext.getBean(FlowRecordAware.class))
                        .block(applicationContext.getBean(ConfigParameterAware.class));
                routerBlockBuilder.children("testcase", "项目测试")
                        .block(applicationContext.getBean(TestCaseAware.class))
                        .block(applicationContext.getBean(TestCaseLogAware.class));
                routerBlockBuilder.children("scheduleJob", "定时任务")
                        .block(applicationContext.getBean(ScheduleJobAware.class))
                        .block(applicationContext.getBean(ScheduleJobLogAware.class));
            }

            @Override
            public DAO getDAO(String daoName) {
                if(FlowMonitorConstant.DAO_NAME.equalsIgnoreCase(daoName)){
                    FlowMonitorOption flowMonitorOption = applicationContext.getBean(FlowMonitorOption.class);
                    CheckInstance.newCheck()
                            .notNullCheck(flowMonitorOption.basePackageName, "基础包名不能为空")
                            .notNullCheck(flowMonitorOption.dao, "数据库对象不能为空")
                            .notNullCheck(flowMonitorOption.logQuickFlow, "流程日志记录对象不能为空")
                            .notNullCheck(flowMonitorOption.testcaseQuickFlow, "测试用例执行对象不能为空")
                            .notNullCheck(flowMonitorOption.scheduleQuickFlow, "定时任务执行对象不能为空");
                    return flowMonitorOption.dao;
                }
                return null;
            }
        };
        return quickAMSListener;
    }
}
